<template>
  <el-form
    ref="ruleFormRef"
    :model="desReqForm"
    :rules="rules"
    class="demo-ruleForm"
    label-position="top"
    size="small"
  >
    <!-- 物体标记号 -->
    <el-form-item
      label="物体标记号"
      prop="Index_Nr"
    >
      <el-input-number
        v-model="desReqForm.Index_Nr"
        :min="0"
      />
    </el-form-item>

    <!-- 设备站点编号 -->
    <el-form-item
      label="设备站点编号"
      prop="Station_Nr"
    >
      <el-input-number
        v-model="desReqForm.Station_Nr"
        :min="0"
      />
    </el-form-item>

    <!-- 报文计数 -->
    <el-form-item
      label="报文计数"
      prop="Tel_Count"
    >
      <el-input-number
        v-model="desReqForm.Tel_Count"
        :min="1"
        :max="30000"
      />
    </el-form-item>

    <!-- 系统运行模式 -->
    <el-form-item
      label="系统运行模式"
      prop="System_Mode"
    >
      <el-input-number
        v-model="desReqForm.System_Mode"
        :min="0"
      />
    </el-form-item>

    <!-- 设备编号 -->
    <el-form-item
      label="设备编号"
      prop="Unit_Nr"
    >
      <el-input-number
        v-model="desReqForm.Unit_Nr"
        :min="0"
      />
    </el-form-item>

    <!-- Spare1~11 -->
    <el-form-item
      v-for="i in 11"
      :key="i"
      :label="`备用字段${i}`"
      :prop="`Spare${i}`"
    >
      <el-input-number
        v-model="desReqForm[`Spare${i}`]"
        :min="0"
      />
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm(ruleFormRef)"
        :disabled="!pickServerStatus"
        :loading="loading"
      >
        发送
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">
        重置
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { sendMessageByServer } from '@/api/pick'
import { pickStore } from '@/store/pickStore'
import { storeToRefs } from 'pinia'

const store = pickStore()
const { pickServerStatus } = storeToRefs(store)

const loading = ref(false)
const ruleFormRef = ref<FormInstance>()

// 表单数据
const desReqForm = reactive({
  Tel_Nr: 7,
  Index_Nr: 0,
  Station_Nr: 0,
  Tel_Count: 0,
  System_Mode: 0,
  Unit_Nr: 0,
  Spare1: 0,
  Spare2: 0,
  Spare3: 0,
  Spare4: 0,
  Spare5: 0,
  Spare6: 0,
  Spare7: 0,
  Spare8: 0,
  Spare9: 0,
  Spare10: 0,
  Spare11: 0,
})

// 校验规则
const rules = reactive<FormRules>({
  telName: [
    { required: true, message: '请输入报文名称', trigger: 'blur' },
    { min: 1, max: 5, message: '长度不超过 5 个字符', trigger: 'blur' }
  ],
  Index_Nr: [{ required: true, message: '请输入物体标记号', trigger: 'blur' }],
  Station_Nr: [{ required: true, message: '请输入设备站点编号', trigger: 'blur' }],
  Tel_Count: [{ required: true, message: '请输入报文计数', trigger: 'blur' }],
  System_Mode: [{ required: true, message: '请输入系统运行模式', trigger: 'blur' }],
  Unit_Nr: [{ required: true, message: '请输入设备编号', trigger: 'blur' }]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      loading.value = true
      sendMessageByServer(desReqForm).then(res => {
        ElMessage({
          showClose: true,
          message: res.data.message,
          type: 'success',
          duration: 1000
        })
      }).finally(() => {
        loading.value = false
      })
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
